package com.zsrt.day20170424.homework;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import org.junit.Test;

/**
*@author jzh
*@创建时间 2017年4月24日下午8:14:27
*类说明：
*/
public class ReadAndWrite {
	
	@Test
	public void readAndWrite() throws IOException{
		File file1=new File("iotest/ask.txt");
		File file2=new File("iotest/answer.txt");
		Reader r=new FileReader(file1);
		Writer w=new FileWriter(file2);
		BufferedReader br=new BufferedReader(r);
		BufferedWriter bw=new BufferedWriter(w);
		String st="";
		int result1=0;
		int result2=0;
		int result3=0;
		int result4=0;
		int result5=0;
		while((st=br.readLine())!=null){
			st=st.trim();
			String st1="[+]";//st1="//+ ";
			String st2="[-]";
			String st3="[*]";
			String st4="[/]";
			String st5="[%]";
			
			if(st.contains("+")){
				String[] s1=st.split(st1);
				int[] num=new int[s1.length];
				for(int i=0;i<s1.length;i++){
					num[i]=Integer.parseInt(s1[i]);
				}
				for(int i=0;i<s1.length;i++){
					result1+=num[i];
				}
				System.out.println(result1);
				bw.write(num[0]+"+"+num[1]+"=");
				bw.write(""+result1);
				bw.flush();
				bw.newLine();
			}else if(st.contains("-")){
				String[] s1=st.split(st2);
				int[] num=new int[s1.length];
				for(int i=0;i<s1.length;i++){
					num[i]=Integer.parseInt(s1[i]);
				}
				result2=num[0]-num[1];
				System.out.println(result2);
				bw.write(num[0]+"-"+num[1]+"=");
				bw.write(""+result2);
				bw.flush();
				bw.newLine();
			}else if(st.contains("*")){
				String[] s1=st.split(st3);
				int[] num=new int[s1.length];
				for(int i=0;i<s1.length;i++){
					num[i]=Integer.parseInt(s1[i]);
				}
				result3=num[0]*num[1];
				System.out.println(result3);
				bw.write(num[0]+"*"+num[1]+"=");
				bw.write(""+result3);
				bw.flush();
				bw.newLine();
			}else if(st.contains("/")){
				String[] s1=st.split(st4);
				int[] num=new int[s1.length];
				for(int i=0;i<s1.length;i++){
					num[i]=Integer.parseInt(s1[i]);
				}
				result4=num[0]/num[1];
				System.out.println(result4);
				bw.write(num[0]+"/"+num[1]+"=");
				bw.write(""+result4);
				bw.flush();
				bw.newLine();
			}else{
				String[] s1=st.split(st5);
				int[] num=new int[s1.length];
				for(int i=0;i<s1.length;i++){
					num[i]=Integer.parseInt(s1[i]);
				}
				result5=num[0]%num[1];
				System.out.println(result5);
				bw.write(num[0]+"%"+num[1]+"=");
				bw.write(""+result5);
				bw.flush();
			}
			
			
		}
		
		bw.close();
		br.close();
	}

}
